﻿using System;
using System.Threading;
using System.Web.UI.WebControls;

namespace Medianamik.UI.Web.MasterPages
{
    public partial class MedianamikExternal : BaseMasterPage
    {
        #region Fields

        private const string UNIQUE_ID = "FC0802C4-DF7F-4851-877C-A95D2EB6A28E";

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AspNetForm.DataBind();

                //Support
                if (Request["showsupportwindow"] == bool.TrueString)
                {
                    SupportRequestWindow.VisibleOnPageLoad = true;
                }
            }

            //Traduction
            if (Request.Params["er"] == null)
            {
                lvwMedianamikCultures.DataSource = Page.MedianamikCultures;
                lvwMedianamikCultures.DataBind();

                if (!Page.IsPostBack)
                {
                    logoMedianamik.Src = "/medianamik/images/Logo_MedianamikLogin_" + Page.UIShortLang + ".gif";
                    Page.DataBind();
                }
            }
            else
            {
                //Il y a eu une erreur d'accès à la base de données. Désactiver la traduction et désactiver le login.
                MainContentHolder.Visible = false;

                panDbError.Visible = true;
                logoMedianamik.Src = "/images/Logo_MedianamikLogin_" + Page.UIShortLang + ".gif";
            }
        }

        //TODO: Améliorer

        #region Lang

        protected void lvwMedianamikCultures_DataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                using (var listViewDataItem = (ListViewDataItem)e.Item)
                {
                    if (listViewDataItem != null)
                    {
                        string culture = listViewDataItem.DataItem.ToString();

                        //Activation du lien sur le nom du node
                        var link = (HyperLink)e.Item.FindControl("lnkCulture");

                        if (link != null)
                        {
                            string shortCultureName = culture.Substring(0, 2).ToUpper();

                            link.NavigateUrl = "javascript:__doPostBack('" + UNIQUE_ID + "','" + culture + "')";
                            link.Text = shortCultureName;
                            link.Enabled = (!culture.Equals(Thread.CurrentThread.CurrentUICulture.Name, StringComparison.OrdinalIgnoreCase));

                            if (!link.Enabled)
                            {
                                link.CssClass = "Actif";
                            }
                        }
                    }
                }
            }
        }

        #endregion

        #endregion

        #region Methods

        

        #endregion

        #region Properties

        protected string SupportRequestWindowUrl
        {
            get
            {
                return "/medianamik/modalwindows/SupportRequestWindow.aspx?" + Request.QueryString;
            }
        }

        #endregion
    }
}
